<?php
namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Loglogin;
use App\Models\RoleUser;
use App\Models\Startup;
use App\Models\User;
use App\Models\UserCategory;
use App\Models\UserInfo;
use App\Models\Zone;
use Illuminate\Http\Request;
use \Excel;

class UserAnalyzeController extends CommonController
{

    /**
     * 获取留存统计页面
     */
    public function getRetain()
    {
        return view('useranalyze.retain');
    }



    /**
     * 次日留存
     */
    public function getDay()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');


        $date = array();
        //获取前31天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 31; $i >= 0; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i > 1 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //第一天的新增的注册用户总数
            $regCount= User::whereBetween('reg_time', [$stamps[$i - 1], $stamps[$i]])->count();
            //第一天新增的注册用户的uid
            $regUsers= User::whereBetween('reg_time', [$stamps[$i - 1], $stamps[$i]])->lists('uid');
            //第二天注册明天首次启动的用户数
            $logCount = Startup::whereBetween('created_at', [$stamps[$i], $stamps[$i+1]])->whereIn('uid', $regUsers)->lists('uid')->toArray();
            $logCount = count(array_flip(array_flip(array_filter($logCount))));

            //日留存率
            if(0==$regCount){
                $counts[] = 0;
            }else{
                $counts[] = ($logCount/$regCount);
            }
        }

        //昨天新增的注册用户总数
        $regCount= User::whereBetween('reg_time', [$today_zero-$day, $today_zero])->count();
        //昨天新增的注册用户的uid
        $regUsers= User::whereBetween('reg_time', [$today_zero-$day, $today_zero])->lists('uid');
        //昨天注册今天首次登录的用户数
        $logCount = Startup::whereBetween('created_at', [$today_zero, time()])->whereIn('uid', $regUsers)->lists('uid')->toArray();
        $logCount = count(array_flip(array_flip(array_filter($logCount))));

        if(0==$regCount){
            $counts[] = 0;
        }else{
            $counts[] = ($logCount/$regCount);
        }

        //获取今天差几个小时
        $rest = sprintf("%.2f", (24 - (time() - $today_zero) / 3600));
        $date[] = '昨天差' . $rest . '小时';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }

    /**
     * 周留存
     */
    public function getWeek()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');


        $date = array();
        //获取前37天到前7天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 37; $i >= 7; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i <37 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //第一天的新增的注册用户总数
            $regCount= User::whereBetween('reg_time', [$stamps[$i - 1], $stamps[$i]])->count();
            //第一天新增的注册用户的uid
            $regUsers= User::whereBetween('reg_time', [$stamps[$i - 1], $stamps[$i]])->lists('uid');
            //第一天注册第二天到第八天登录的用户数
            $logCount = Startup::whereBetween('created_at', [$stamps[$i], $stamps[$i]+7*$day])->whereIn('uid', $regUsers)->lists('uid')->toArray();
            $logCount = count(array_flip(array_flip(array_filter($logCount))));

            //周留存率
            if(0==$regCount){
                $counts[] = 0;
            }else{
                $counts[] = ($logCount/$regCount);
            }
        }

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }


    /**
     * 月留存
     */
    public function getMonth()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');


        $date = array();
        //获取前60天到前30天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 60; $i >= 30; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i <60 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //第一天的新增的注册用户总数
            $regCount= User::whereBetween('reg_time', [$stamps[$i - 1], $stamps[$i]])->count();
            //第一天新增的注册用户的uid
            $regUsers= User::whereBetween('reg_time', [$stamps[$i - 1], $stamps[$i]])->lists('uid');
            //第一天注册第二天到第八天登录的用户数
            $logCount = Startup::whereBetween('created_at', [$stamps[$i], $stamps[$i]+30*$day])->whereIn('uid', $regUsers)->lists('uid')->toArray();
            $logCount = count(array_flip(array_flip(array_filter($logCount))));

            //月留存率
            if(0==$regCount){
                $counts[] = 0;
            }else{
                $counts[] = ($logCount/$regCount);
            }
        }

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }


    /**
     * 获取活跃统计页面
     */
    public function getActive()
    {
        return view('useranalyze.active');
    }

    /**
     *获取启动量统计页面
     */
    public function getStartup()
    {
        return view('useranalyze.startup');
    }


    /**
     * 日活跃用户
     */
    public function getDayActive()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');
        //获取今天差几个小时
        $rest = sprintf("%.2f", (24 - (time() - $today_zero) / 3600));

        $date = array();
        //获取前31天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i > 0 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //获取前三十天的登录数据，过去的数据放在前面
            $uids = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->lists('uid')->toArray();

            $counts[] = count(array_flip(array_flip(array_filter($uids))));
        }

        $today_uids = User::whereBetween('reg_time', [$today_zero, time()])->lists('uid')->toArray();
        $counts[]= count(array_flip(array_flip(array_filter($today_uids))));
        $date[] = '今天差' . $rest . '小时';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }


    /**
     * 周活跃用户
     */
    public function getWeekActive()
    {
        //获取本工作周还剩下几天
        $rest = (6 - date('w')) % 7;
        $rest == 0 && $rest = 7;


        //获取上周六凌晨时间戳
        $thisSta = strtotime('today') - (7 - $rest) * 24 * 3600;

        // 定义一周的时间戳
        $week = 7 * 24 * 3600;

        //获取前30周的每周周六凌晨的时间戳,从前往后排
        $stamps = array();
        $date = array();

        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $thisSta - $i * $week;
            $i > 0 && $date[] = '前' . ($i) . '周';
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            $uids = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->lists('uid')->toArray();
            $counts[] = count(array_flip(array_flip(array_filter($uids))));
        }
        $this_uids = Startup::whereBetween('created_at',  [$thisSta, time()])->lists('uid')->toArray();
        $counts[] = count(array_flip(array_flip(array_filter($this_uids))));
        $date[] = '本工作周差' . $rest . '天';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }

    /**
     * 月活跃用户
     */
    public function getMonthActive()
    {
        //获取本月还剩下多少天
        $rest = date('t') - date('j') + 1;
        //获取本月1号凌晨的时间戳
        $thisMonth = strtotime(date('Y-m-01'));
        //获取过去十三个月的，每个月1号凌晨的时间戳，从前往后
        $m = date('m');
        $y = date('Y');
        $stamps = array();
        for ($i = 0; $i < 13; $i++) {
            $newM = $m - $i;
            if ($newM <= 0) {
                $newM += 12;
                $y = $y - 1;
            }
            $stamps[] = strtotime($y . '-' . $newM . '-01');
            $y = date('Y');
        }

        $counts = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的登录数据，过去的数据放在前面
            $uids = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->lists('uid')->toArray();
            $counts[] = count(array_flip(array_flip(array_filter($uids))));
        }

        $date = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的文字表述，过去的表述放在前面
            $date[] = $m - $i < 1 ? '去年' . ($m - $i + 12) . '月' : $m - $i . '月';
        }

        $this_uids = Startup::whereBetween('created_at',  [$thisMonth, time()])->lists('uid')->toArray();
        $counts[] = count(array_flip(array_flip(array_filter($this_uids))));
        $date[] = '本月差' . $rest . '天';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }


    /**
     * 沉默用户 目前连续45天没有登录的会员
     */
    public function getSilence()
    {
        //获取45天前的时间戳
        $before = time()-24*3600*45;

        $uids = Startup::whereBetween('created_at', [$before, time()])->lists('uid')->toArray();
//        dd($uids);

        $uids =array_flip(array_flip(array_filter($uids)));
//
//        $l_uids = Loglogin::whereBetween('created_at', [$before, time()])->lists('uid')->toArray();
//
//        $l_uids =array_flip(array_flip(array_filter($l_uids)));
//        dd($l_uids);

        $users = User::with('roleUser.role')->whereNotIn('uid', $uids)->paginate(10);

//        dd($users);
        return view('useranalyze.silence')->with("users", $users);
    }

    /**
     * 日启动用户
     */
    public function getDayStart()
    {
        $day = 24 * 3600;
        $today_zero = strtotime('today');
        //获取今天差几个小时
        $rest = sprintf("%.2f", (24 - (time() - $today_zero) / 3600));

        $date = array();
        //获取前31天的每天凌晨的时间戳,过去的时间排在前面
        $stamps = array();
        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $today_zero - $i * $day;
            $i > 0 && $date[] = date('m-d', strtotime(-$i . " day"));
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            //获取前三十天的首次启动的数据，过去的数据放在前面
            //ios端
            $ios_count = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->where('is_first', 1)->where('client', 8)->count();

            $ad_count = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->where('is_first', 1)->where('client', 7)->count();

            $total = $ios_count+$ad_count;
            $result = array('total' => $total, 'ios' => $ios_count, 'android' => $ad_count);

            $counts[] = $result;
        }

        $today_ios_count = Startup::whereBetween('created_at', [$today_zero, time()])->where('is_first', 1)->where('client', 8)->count();
        $today_ad_count = Startup::whereBetween('created_at', [$today_zero, time()])->where('is_first', 1)->where('client', 7)->count();
        $today_total_count = $today_ios_count+$today_ad_count;
        $today_result = array('total' => $today_total_count,'ios' => $today_ios_count, 'android' => $today_ad_count);

        $counts[]= $today_result;
        $date[] = '今天差' . $rest . '小时';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }

    /**
     * 周启动用户
     */
    public function getWeekStart()
    {
        //获取本工作周还剩下几天
        $rest = (6 - date('w')) % 7;
        $rest == 0 && $rest = 7;


        //获取上周六凌晨时间戳
        $thisSta = strtotime('today') - (7 - $rest) * 24 * 3600;

        // 定义一周的时间戳
        $week = 7 * 24 * 3600;

        //获取前30周的每周周六凌晨的时间戳,从前往后排
        $stamps = array();
        $date = array();

        for ($i = 30; $i >= 0; $i--) {
            $stamps[] = $thisSta - $i * $week;
            $i > 0 && $date[] = '前' . ($i) . '周';
        }

        $counts = array();
        for ($i = 1; $i < 31; $i++) {
            $ios_count = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->where('is_first', 1)->where('client', 8)->count();

            $ad_count = Startup::whereBetween('created_at', [$stamps[$i - 1], $stamps[$i]])->where('is_first', 1)->where('client', 7)->count();

            $total = $ios_count+$ad_count;
            $result = array('total' => $total, 'ios' => $ios_count, 'android' => $ad_count);

            $counts[] = $result;
        }


        $today_ios_count = Startup::whereBetween('created_at', [$thisSta, time()])->where('is_first', 1)->where('client', 8)->count();
        $today_ad_count = Startup::whereBetween('created_at', [$thisSta, time()])->where('is_first', 1)->where('client', 7)->count();
        $today_total_count = $today_ios_count+$today_ad_count;
        $today_result = array('total' => $today_total_count,'ios' => $today_ios_count, 'android' => $today_ad_count);

        $counts[]= $today_result;
        $date[] = '本工作周差' . $rest . '天';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }

    /**
     * 月首次启动数
     */
    public function getMonthStart()
    {
        //获取本月还剩下多少天
        $rest = date('t') - date('j') + 1;
        //获取本月1号凌晨的时间戳
        $thisMonth = strtotime(date('Y-m-01'));
        //获取过去十三个月的，每个月1号凌晨的时间戳，从前往后
        $m = date('m');
        $y = date('Y');
        $stamps = array();
        for ($i = 0; $i < 13; $i++) {
            $newM = $m - $i;
            if ($newM <= 0) {
                $newM += 12;
                $y = $y - 1;
            }
            $stamps[] = strtotime($y . '-' . $newM . '-01');
            $y = date('Y');
        }

        $counts = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的登录数据，过去的数据放在前面
            $ios_count = Startup::whereBetween('created_at', [$stamps[$i], $stamps[$i-1]])->where('is_first', 1)->where('client', 8)->count();

            $ad_count = Startup::whereBetween('created_at', [$stamps[$i], $stamps[$i-1]])->where('is_first', 1)->where('client', 7)->count();

            $total = $ios_count+$ad_count;
            $result = array('total' => $total, 'ios' => $ios_count, 'android' => $ad_count);

            $counts[] = $result;
        }

        $date = array();
        for ($i = 12; $i > 0; $i--) {
            //获取前12个月的文字表述，过去的表述放在前面
            $date[] = $m - $i < 1 ? '去年' . ($m - $i + 12) . '月' : $m - $i . '月';
        }



        $today_ios_count = Startup::whereBetween('created_at', [$thisMonth, time()])->where('is_first', 1)->where('client', 8)->count();
        $today_ad_count = Startup::whereBetween('created_at', [$thisMonth, time()])->where('is_first', 1)->where('client', 7)->count();
        $today_total_count = $today_ios_count+$today_ad_count;
        $today_result = array('total' => $today_total_count,'ios' => $today_ios_count, 'android' => $today_ad_count);

        $counts[]= $today_result;
        $date[] = '本月差' . $rest . '天';

        $arr = [];
        foreach ($date as $k => $i) {
            $arr[] = array($i, $counts[$k]);
        }

        echo json_encode($arr);
    }



    /**
     * 超级用户
     */
    public function getSuperActive()
    {
        echo 1;
    }





}